// pc端固定客户端授权id
import { AxiosPromise } from 'axios'
import request from '@/hooks/utils/http/index'
import { UserInfo } from '@/service/UserService'

const clientId = import.meta.env.VITE_GLOB_APP_CLIENT_ID

/**
 * @param data {LoginData}
 * @returns
 */
export class LoginService {
    static login(data: LoginData): AxiosPromise<LoginResult> {
        const params = {
            ...data,
            clientId: data.clientId || clientId,
            grantType: data.grantType || 'password'
        }
        return request({
            url: '/auth/login',
            headers: {
                isToken: false,
                isEncrypt: true,
                repeatSubmit: false
            },
            method: 'post',
            data: params
        })
    }

// 注册方法
    static register(data: any) {
        const params = {
            ...data,
            clientId: clientId,
            grantType: 'password'
        }
        return request({
            url: '/auth/register',
            headers: {
                isToken: false,
                isEncrypt: true,
                repeatSubmit: false
            },
            method: 'post',
            data: params
        })
    }

    /**
     * 注销
     */
    static logout() {
        if (import.meta.env.VITE_APP_SSE === 'true') {
            request({
                url: '/resource/sse/close',
                method: 'get'
            })
        }
        return request({
            url: '/auth/logout',
            method: 'post'
        })
    }

    /**
     * 获取验证码
     */
    static getCodeImg(): AxiosPromise<VerifyCodeResult> {
        return request({
            url: '/auth/code',
            headers: {
                isToken: false
            },
            method: 'get',
            timeout: 20000
        })
    }

    /**
     * 第三方登录
     */
    static callback(data: LoginData): AxiosPromise<any> {
        const LoginData = {
            ...data,
            clientId: clientId,
            grantType: 'social'
        }
        return request({
            url: '/auth/social/callback',
            method: 'post',
            data: LoginData
        })
    }

// 获取用户详细信息
    static getInfo(): AxiosPromise<UserInfo> {
        return request({
            url: '/system/user/getInfo',
            method: 'get'
        })
    }

// 获取租户列表
    static getTenantList(isToken: boolean): AxiosPromise<TenantInfo> {
        return request({
            url: '/auth/tenant/list',
            headers: {
                isToken: isToken
            },
            method: 'get'
        })
    }
}

/**
 * 注册
 */
export type RegisterForm = {
    tenantId: string;
    username: string;
    password: string;
    confirmPassword?: string;
    code?: string;
    uuid?: string;
    userType?: string;
};

/**
 * 登录请求
 */
export interface LoginData {
    tenantId?: string;
    username?: string;
    password?: string;
    rememberMe?: boolean;
    socialCode?: string;
    socialState?: string;
    source?: string;
    code?: string;
    uuid?: string;
    clientId: string;
    grantType: string;
}

/**
 * 登录响应
 */
export interface LoginResult {
    access_token: string;
}

/**
 * 验证码返回
 */
export interface VerifyCodeResult {
    captchaEnabled: boolean;
    uuid?: string;
    img?: string;
}

/**
 * 租户
 */
export interface TenantVO {
    companyName: string;
    domain: any;
    tenantId: string;
}

export interface TenantInfo {
    tenantEnabled: boolean;
    voList: TenantVO[];
}
